<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Escort_visitted_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $escort_id = 0;
	var $number_visitted = 0;
	var $datetime = 0; //timestamp day
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_ESCORT_VISITTED)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->escort_id = $result[0]->escort_id;
			$this->number_visitted = $result[0]->number_visitted;
			$this->datetime = $result[0]->datetime;
		
			return $this;
		}
		return false;
	}
	
	function setEscortId($input){ $this->escort_id=$input; }
	function setNumberVisitted($input){ $this->number_visitted=$input; }
	function setDateTime($input){ $this->datetime=$input; }
	
	function getId(){return $this->id;}
	function getEscortId(){return $this->escort_id;}
	function getNumberVisitted(){ return $this->number_visitted; }
	function getDateTime(){return $this->datetime;}
	
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('escort_id',$this->getEscortId());
		$ci->db->set('number_visitted',$this->getNumberVisitted());
		$ci->db->set('datetime',$this->getDateTime());
		
		return $ci->db->where('id', $this->id)->update(TBL_ESCORT_VISITTED);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('number_visitted',0);
		$ci->db->insert(TBL_ESCORT_VISITTED);
		return $ci->db->insert_id();
	}
	
	function findObjOfEscortBaseCurrentDateTime($escort_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$dateTimeStamp = getTimeStampDateOfCurrentTime(time());
		$rs = $ci->db->where(  array('escort_id'=>$escort_id, 'datetime'=>$dateTimeStamp) )->get(TBL_ESCORT_VISITTED)->result();
		if(!$rs) return false;
		return $rs[0];
	}
	
	function increaseNumberVisittedOfEscort($escort_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$obj = $this->findObjOfEscortBaseCurrentDateTime($escort_id);
		if($obj){
			$ci->db->set('number_visitted',1+$obj->number_visitted);
			$ci->db->where('id', $obj->id)->update(TBL_ESCORT_VISITTED);
		}else{
			$dateTimeStamp = getTimeStampDateOfCurrentTime(time());
			
			$newid = $this->createNewEntity();
			$ci->db->set('escort_id',$escort_id);
			$ci->db->set('number_visitted',1);
			$ci->db->set('datetime',$dateTimeStamp);
		
			$ci->db->where('id', $newid )->update(TBL_ESCORT_VISITTED);
		}
	}
	
	function countNumberVisittedOfEscortInMonth($escort_id, $timeStamp){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$monthTimeStamp = array_values( getTimeStampMonthOfCurrentTime($timeStamp) );
		
		list($from, $to) = $monthTimeStamp;
		$rs = $ci->db->
			query("SELECT SUM(number_visitted) AS total FROM ". TBL_ESCORT_VISITTED 
					." WHERE escort_id=$escort_id AND datetime >= $from AND datetime <= $to")
			->result();
		if($rs) return $rs[0]->total;
		
		return 0;
	}
	
	function arrayVisittedOfEscortAllTime($escort_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$rs = $ci->db->where('escort_id', $escort_id)->order_by('datetime','asc')->get(TBL_ESCORT_VISITTED)->result();
		if(!$rs) return false;
		//$min_time = $rs[0]->datetime;
		//$max_time = time();
		
		$array_month_value = array();
		foreach($rs as $item){
			$monthTimeStamp = array_values( getTimeStampMonthOfCurrentTime($item->datetime) );
			list($from, $to) = $monthTimeStamp;
			$key = $from.'_'.$to;
			if(! isset($array_month_value[$key])){
				$array_month_value[$key] = $item->number_visitted;
			}else{
				$array_month_value[$key] += $item->number_visitted;
			}
		}
		return $array_month_value;
	}
	
	
	
	
	
//endclass

}	